//
//  AuthenCompanyLegalMsgVM.swift
//  kxf
//
//  Created by Mac on 2024/3/8.
//

import Foundation
import UIKit
import SwiftUI

class AuthenCompanyLegalMsgVM: UIView {
    
    var selfHeight = RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*5
    
    var controller = WHBaseViewVC()
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var titleVm : RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.titleLabel.text = "法人信息"
        return vm
    }()
    lazy var idNameVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.titleVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "法人姓名"
        vm.textField.placeholder = "请输入法人姓名"
        vm.textField.keyboardType = .default
        vm.maxLength = 30
        return vm
    }()
    lazy var idNoVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idNameVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "证件号码"
        vm.textField.placeholder = "请输入证件号码"
//        vm.textField.keyboardType = .default
        vm.maxLength = 30
        return vm
    }()
    lazy var idStartTimeVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idNoVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "证件有效期始"
        vm.textField.placeholder = "请选择证件有效期始"
        vm.showArrowImg()
        vm.tapBlock = {()in
            self.choiceTimeAction()
        }
        return vm
    }()
    lazy var idEndTimeVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idStartTimeVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "证件有效期至"
        vm.textField.placeholder = "请选择证件有效期至"
        vm.showArrowImg()
        vm.tapBlock = {()in
            self.choiceTimeEndAction()
        }
        return vm
    }()
    lazy var idAddressVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idEndTimeVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "身份证地址"
        vm.textField.placeholder = "请输入身份证地址"
        vm.textField.keyboardType = .default
        vm.maxLength = 50
//        vm.showArrowImg()
        return vm
    }()
    lazy var datePickerVm : DatePickerVM = {
        let vm = DatePickerVM.init(frame: .zero)
        vm.isHidden = true
        
        return vm
    }()
}

extension AuthenCompanyLegalMsgVM{
    func initUI() {
        addSubview(titleVm)
        addSubview(idNameVm)
        addSubview(idNoVm)
        addSubview(idStartTimeVm)
        addSubview(idEndTimeVm)
        addSubview(idAddressVm)
        UIApplication.shared.keyWindow?.addSubview(datePickerVm)
    }
    func updateUIForRefuse() {
        idNameVm.textField.text = "\(AuthenCompanyMsgModel.shareInstance.legalName)"
        idNoVm.textField.text = "\(AuthenCompanyMsgModel.shareInstance.legalIdCardNo)"
        idStartTimeVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.legalIdCardStartTime)")
        idEndTimeVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.legalIdCardEndTime)")
        idAddressVm.textField.text = AuthenCompanyMsgModel.shareInstance.legalAddress
    }
}

extension AuthenCompanyLegalMsgVM{
    func choiceTimeAction(){
        self.datePickerVm.isHidden = false
        self.datePickerVm.titleLabel.text = "证件有效期始"
        self.datePickerVm.timeConfirmBlock = {(time)in
            AuthenMiniMsgModel.shareInstance.legalIdCardStartTime = time
//            self.idStartTimeVm.textField.text = time
            self.idStartTimeVm.setLabelContent(content: time)
            self.choiceTimeEndAction()
        }
    }
    func choiceTimeEndAction(){
        let alertVc = UIAlertController(title: "身份证是否长期有效", message: "", preferredStyle: .alert)
        
        let okAction = UIAlertAction(title: "长期有效", style: .default, handler: { (action)in
            AuthenMiniMsgModel.shareInstance.legalIdCardEndTime = "9999-01-01"
            self.idEndTimeVm.setLabelContent(content: AuthenMiniMsgModel.shareInstance.legalIdCardEndTime)
        })
        alertVc.addAction(okAction)
        let cancelAction = UIAlertAction(title: "非长期", style: .cancel, handler:{ (action)in
            self.datePickerVm.isHidden = false
            self.datePickerVm.titleLabel.text = "证件有效期至"
            self.datePickerVm.timeConfirmBlock = {(time)in
                AuthenMiniMsgModel.shareInstance.legalIdCardEndTime = time
//                self.idEndTimeVm.textField.text = AuthenMiniMsgModel.shareInstance.legalIdCardEndTime
                self.idEndTimeVm.setLabelContent(content: AuthenMiniMsgModel.shareInstance.legalIdCardEndTime)
            }
        })
        alertVc.addAction(cancelAction)
        self.controller.present(alertVc, animated:true, completion:nil)
    }
}
